package cn.layfolk.seckill.service.impl;

import cn.layfolk.seckill.domain.TSeckillOrder;
import cn.layfolk.seckill.mapper.TSeckillOrderMapper;
import cn.layfolk.seckill.redis.SeckillRedisKey;
import cn.layfolk.seckill.service.TSeckillOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-01-10
 */
@Service
public class TSeckillOrderServiceImpl extends ServiceImpl<TSeckillOrderMapper, TSeckillOrder> implements TSeckillOrderService {


    @Autowired
    private TSeckillOrderMapper seckillOrderMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void create(String orderNo, Long userId, Long seckillId) {
        TSeckillOrder seckillOrder = new TSeckillOrder();
        seckillOrder.setOrderNo(orderNo);
        seckillOrder.setUserId(userId);
        seckillOrder.setSeckillId(seckillId);
        seckillOrderMapper.insert(seckillOrder);
        // 将当前秒杀成功的用户，保存到redis中
        stringRedisTemplate.opsForSet().add(
                SeckillRedisKey.SECKILL_ORDER_SET.getRealKey(seckillId+""),userId+"");
    }
}
